﻿using System;
using System.Windows.Forms;
using Alerter.DataSet;
using DevExpress.XtraEditors;
using E4RPTools;

namespace Alerter.Controls
{
    public partial class QueryXtraForm : XtraForm
    {
        private long _documentId;
        private Controller _controller;
        private QueryDataSet _dataSet;

        #region Properties

        public long DocumentId
        {
            set { _documentId = value; }
        }

        public Controller Controller
        {
            set { _controller = value; }
        }

        #endregion

        public QueryXtraForm()
        {
            InitializeComponent();
            Load += QueryXtraFormLoad;
            okSimpleButton.Click += OkSimpleButtonClick;
            checkSimpleButton.Click += CheckSimpleButtonClick;
            executeSimpleButton.Click += ExecuteSimpleButtonClick;
            memoEdit.TextChanged += MemoEditTextChanged;
        }

        #region Private Methods

        private void MergeData()
        {
            _dataSet = new QueryDataSet();
            if (_documentId <= 0)
            {
                var document = _dataSet.Query.NewQueryRow();
                document.Id = _documentId;
                document.Nome = "";
                document.Comando = "";
                _dataSet.Query.AddQueryRow(document);
            }
            else
                _dataSet.Merge(_controller.GetQuery(_documentId));
        }

        private void GetDataFromControls()
        {
            _dataSet.Query[0].Nome = nameTextEdit.Text;
            _dataSet.Query[0].Comando = memoEdit.Text;
        }

        private void SetDataToControls()
        {
            nameTextEdit.Text = _dataSet.Query[0].Nome;
            memoEdit.Text = _dataSet.Query[0].Comando;            
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(nameTextEdit, "");
            dxErrorProvider.SetError(memoEdit, "");
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Nome":
                    return nameTextEdit;
                case "Comando":
                    return memoEdit;
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (var error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        #endregion

        void QueryXtraFormLoad(object sender, EventArgs e)
        {
            MergeData();
            SetDataToControls();
        }

        void MemoEditTextChanged(object sender, EventArgs e)
        {
            checkSimpleButton.Enabled = memoEdit.Text.Length > 0;
            executeSimpleButton.Enabled = memoEdit.Text.Length > 0;
        }

        void OkSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            Result result = _controller.IsQueryValid(_dataSet);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SaveQuery(_dataSet);
        }

        void CheckSimpleButtonClick(object sender, EventArgs e)
        {
            var result = _controller.IsQueryComandValid(memoEdit.Text);
            MessageBox.Show(result.Errors[0].Message, "Validar Comando", MessageBoxButtons.OK,
                            result.Errors[0].Reference.Length == 0 ? MessageBoxIcon.Information : MessageBoxIcon.Error);
        }

        void ExecuteSimpleButtonClick(object sender, EventArgs e)
        {
            var window = new QueryResultXtraForm { OperatorData = _controller.GetOperators(), ContactsData = _controller.RunQueryComand(memoEdit.Text)};
            window.ShowDialog();
        }

    }
}
